﻿// Decompiled by AS3 Sorcerer 6.78
// www.buraks.com/as3sorcerer

//com.wdcgame.tile.model.Tileset

package com.game.tile.model
{
    import base.BaseVO;

    import flash.display.BitmapData;
    import flash.utils.Dictionary;
    import flash.geom.Rectangle;
    import flash.geom.Point;

    public class Tileset extends BaseVO
    {

        public var firstgid:int;
        public var name:String;
        public var tilewidth:Number;
        public var tileheight:Number;
        public var image:String;
        public var imageWidth:Number;
        public var imageHeight:Number;
        public var spacing:int;
        public var margin:int;
        public var tileoffsetX:int;
        public var tileoffsetY:int;
        public var path:String;
        public var tiles:Vector.<TileVO>;
        public var bitmapData:BitmapData;
        public var tileBiitmapDatas:Dictionary;


        override public function buildFromXMLAttr(_arg_1:XML):void
        {
            var _local_2:* = null;
            super.buildFromXMLAttr(_arg_1);
            image = _arg_1.image.@source;
            imageWidth = Number(_arg_1.image.@width);
            imageHeight = Number(_arg_1.image.@height);
            tileoffsetX = _arg_1.tileoffset.@x;
            tileoffsetY = _arg_1.tileoffset.@y;
            tiles = new Vector.<TileVO>();
            for each (var _local_3:XML in _arg_1.tile)
            {
                _local_2 = new TileVO();
                _local_2.buildFormXML(_local_3);
                tiles.push(_local_2);
            }
        }

        public function getBitmapData(_arg_1:int):BitmapData
        {
            var _local_3:int;
            var _local_7:int;
            var _local_5:int;
            var _local_6:* = null;
            var _local_2:* = null;
            var _local_4:* = null;
            if (!tileBiitmapDatas)
            {
                tileBiitmapDatas = new Dictionary();
                _local_3 = firstgid;
                _local_7 = 0;
                while (_local_7 < (imageHeight / tileheight))
                {
                    _local_5 = 0;
                    while (_local_5 < (imageWidth / tilewidth))
                    {
                        _local_6 = new BitmapData(tilewidth, tileheight, true, 0);
                        _local_2 = new Rectangle((_local_5 * tilewidth), (_local_7 * tileheight), tilewidth, tileheight);
                        if ((_local_2.x + _local_2.width) > imageWidth)
                        {
                            _local_2.width = (imageWidth - _local_2.x);
                        }
                        if ((_local_2.y + _local_2.height) > imageHeight)
                        {
                            _local_2.height = (imageHeight - _local_2.y);
                        }
                        _local_4 = bitmapData.getPixels(_local_2);
                        _local_4.position = 0;
                        _local_6.setPixels(new Rectangle(0, 0, _local_2.width, _local_2.height), _local_4);
                        tileBiitmapDatas[_local_3] = _local_6;
                        _local_3++;
                        _local_5++;
                    }
                    _local_7++;
                }
            }
            return (tileBiitmapDatas[_arg_1]);
        }

        public function getXY(_arg_1:int):Point
        {
            var _local_11:* = null;
            var _local_10:int;
            var _local_8:int;
            var _local_7:int = firstgid;
            var _local_6:int = int((imageHeight / tileheight));
            var _local_5:int = int((imageWidth / tilewidth));
            var _local_2:Array = [];
            var _local_9:int = firstgid;
            _local_10 = 0;
            while (_local_10 < _local_6)
            {
                _local_8 = 0;
                while (_local_8 < _local_5)
                {
                    _local_11 = new Point();
                    _local_11.x = _local_8;
                    _local_11.y = _local_10;
                    _local_2[_local_9] = _local_11;
                    _local_9++;
                    _local_8++;
                }
                _local_10++;
            }
            var _local_3:int = _local_2[_arg_1].x;
            var _local_4:int = _local_2[_arg_1].y;
            return (new Point((_local_3 * tilewidth), (_local_4 * tileheight)));
        }

        public function contain(_arg_1:int):Boolean
        {
            var _local_2:int = int((((imageHeight / tileheight) * (imageWidth / tilewidth)) + firstgid));
            if (((_arg_1 < _local_2) && (_arg_1 >= firstgid)))
            {
                return (true);
            }
            return (false);
        }

        public function getTileById(_arg_1:int):TileVO
        {
            var _local_2:int;
            _local_2 = 0;
            while (_local_2 < tiles.length)
            {
                if (tiles[_local_2].id == _arg_1)
                {
                    return (tiles[_local_2]);
                }
                _local_2++;
            }
            return (null);
        }


    }
}//package com.wdcgame.tile.model

